package com.wh.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test5 {

    public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {

        Class c=User.class;

        //获取类的名称
        System.out.println(c.getName());//com.wh.reflection.User
        System.out.println(c.getSimpleName());//User

        System.out.println("===================================");
        //获取类的属性
        Field[] fields = c.getFields();
        for (Field field : fields) {
            System.out.println(field);//只能获得public的
        }
        fields=c.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);//能获取全部的属性
        }

        //获取指定的属性
        Field name = c.getDeclaredField("name");
        System.out.println(name);

        System.out.println("===================================");

        //获取类的方法
        Method[] methods = c.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        methods=c.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        //获取指定方法
        Method getName = c.getMethod("getName");
        System.out.println(getName);
        Method setName = c.getMethod("setName", String.class);
        System.out.println(setName);

        System.out.println("===================================");

        //获取构造器
        Constructor[] constructors = c.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        //获取指定构造器
        Constructor constructor = c.getConstructor(String.class, Integer.class);
        System.out.println(constructor);

    }
}
